package com.pure.database.ui.component

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.pure.database.greendao.GreenDaoHelper
import com.pure.database.greendao.entity.GDOrder
import com.pure.database.greendao.entity.GDUser
import com.pure.database.ui.common.style.NormalButton
import kotlin.random.Random


/**
 * FileName : GreenDaoTest
 * Author : luqihua
 * Date : 2025/3/20
 * Desc :
 **/

private const val TAG = "GreenDaoTest"

@Composable
fun GreenDaoTest(modifier: Modifier = Modifier) {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {


        val random = Random(1)
        Column {
            NormalButton(text = "插入用户") {
                val user = GDUser(0L, "张三", random.nextInt())
                val id = GreenDaoHelper.getDaoSession().gdUserDao.insert(user)
                Log.d(TAG, "insert User id=${id}")
            }

            NormalButton(text = "查询User") {
                GreenDaoHelper.getDaoSession().gdUserDao.queryBuilder().list().forEach() {
                    Log.d(TAG, "user = $it")
                    Log.d(TAG, "user.orders = ${it.orders}")
                }
            }

            NormalButton(text = "插入订单") {
                val order = GDOrder().also {
                    it.name = "订单${random.nextInt()}"
                    it.userId = 0L
                    it.amount = random.nextInt(1000)
                }

                val id = GreenDaoHelper.getDaoSession().gdOrderDao.insert(order)
                Log.d(TAG, " insert order id=${id}")
            }

            NormalButton(text = "查询订单") {
                GreenDaoHelper.getDaoSession().gdOrderDao.queryBuilder().list().forEach() {
                    Log.d(TAG, "order = $it")
                }
            }
        }

    }
}